package com.ruoyi.web.controller.demo;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.SM4Utils;
import com.ruoyi.demo.domain.EncryptionTable;
import com.ruoyi.demo.mapper.EncryptionTableMapper;
import com.ruoyi.demo.service.EncryptionTableService;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.mapper.SysPostMapper;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/demo/gzj")
public class DemoController extends BaseController {


    @Autowired
    private EncryptionTableService encryptionTableService;

    @Autowired
    private EncryptionTableMapper encryptionTableMapper;

    @Autowired
    private SysPostMapper sysPostMapper;


    @GetMapping("/tt")
    @Anonymous
    public AjaxResult tt() throws Exception {

        SM4Utils.genKey();

        String encrypt = SM4Utils.encrypt("12345645645645641561655445");
        String encrypt1 = SM4Utils.encrypt("南京市精神病医院");

        String decrypt = SM4Utils.decrypt(encrypt);
        String decrypt1 = SM4Utils.decrypt(encrypt1);

        SysPost sysPost = sysPostMapper.selectPostById(Long.valueOf("5"));
        log.info("原文：{},密文：{}",decrypt,encrypt);
        log.info("原文：{},密文：{}",decrypt1,encrypt1);

        return success(sysPost);
    }

    @GetMapping
    public AjaxResult selectAll(){
        startPage();
        List<EncryptionTable> list = encryptionTableService.list();
        return success(list);
    }

    @PostMapping("/insert")
    public AjaxResult insert(@RequestBody EncryptionTable encryptionTable){
        boolean save = encryptionTableService.save(encryptionTable);
        return success(save);
    }

    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable String id){
        boolean b = encryptionTableService.removeById(id);
        return success(b);
    }

    @PutMapping
    public AjaxResult update(@RequestBody EncryptionTable encryptionTable){
        boolean b = encryptionTableService.updateById(encryptionTable);
        return success(b);
    }

    @GetMapping("/getTableNames")
    public AjaxResult getTableNames(){
        List<Map<String, String>> tableName = encryptionTableMapper.getTableName();
        return success(tableName);
    }


    @GetMapping("/getTableColumns/{tableName}")
    public AjaxResult getTableNames(@PathVariable String tableName){
        List<Map<String, String>> tableColumn = encryptionTableMapper.getTableColumn(tableName);
        System.err.println(tableColumn);

        return success(tableColumn);
    }

    @ApiOperation("开启SM4加解密")
    @PostMapping("/SM4Encry")
    public AjaxResult SM4Encry(){
        encryptionTableService.SM4DataEncrypDecryp();
        return success();
    }
}
